﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CoreProcess;
using CoreProcess.Resources;
using OpenFileDialog = Microsoft.Win32.OpenFileDialog;

namespace Personal_Resource_Management.View
{
    /// <summary>
    /// Interaction logic for ResourceView.xaml
    /// </summary>
    public partial class ResourceView : Window
    {

        private ResourceService resourceService;

        private ResourceRoot resourceRoot;

        public ResourceView()
        {
            InitializeComponent();
            resourceService = new ResourceService();
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {


            //ObservableCollection<FileItem> dsfItems = new List<fiel
            //this.dataGrid1.DataContext = 
        }



        private void buttonSelectResource_Click(object sender, RoutedEventArgs e)
        {
            var fileselect =
                new OpenFileDialog();

            if (fileselect.ShowDialog().Value)
            {
                resourceRoot =
                    resourceService.LoadResource(fileselect.FileName);

                this.labelResourceName.Content = resourceRoot.RootName;

                setDataGrid(resourceRoot.Items);
                //this.comboBox1.DataContext = resourceService.GetFileTypeList(res);
                this.listBox1.DataContext = resourceService.GetFileTypeList(resourceRoot).Select(x => x.ToString());
                //this.listView1.DataContext = resourceService.GetFileTypeList(res);
            }

            
        }

        private void setDataGrid(List<FileItem> data)
        {
            var dc =
                new ObservableCollection<FileItem>(data);

            this.dataGrid1.DataContext = dc;

            this.labelCount.Content = dc.Count;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (resourceRoot == null)
                return;

            List<FileItem> filterList = resourceRoot.Items;

            //type filter
            var typelist =
                listBox1.SelectedItems
                            .Cast<string>().ToList()
                                        .Select(x => x.Split('#')[0]).ToList();
            if (typelist.Any())
                filterList = resourceRoot.Items.Where(item => typelist.Contains(item.FileType)).ToList(); 
                

            //Full Name filter
            if (!string.IsNullOrEmpty(textBoxFIleFullName.Text))
                filterList = filterList.Where(item => item.FilePath.ToLower().Contains(textBoxFIleFullName.Text.ToLower())).ToList();

            setDataGrid(filterList);

            // 

            //resourceRoot.Items.Where()

            //this.dataGrid1.DataContext 

            //resourceService.File
        }
    }
}
